/*
 * bsp_can.c
 *
 *  Created on: Jun 29, 2025
 *      Author: User
 */

#include "bsp_can.h"

CAN_TxHeaderTypeDef TxHeader;
CAN_TxHeaderTypeDef Tx2Header;
uint8_t TxData[8];
uint8_t Tx2Data[8];
uint32_t TxMailbox;
uint32_t Tx2Mailbox;

void BSP_CAN1_Config(void) {
	/*##-1- Configure the CAN peripheral #######################################*/
	MX_CAN1_Init();
	/*##-2- Configure the CAN Filter ###########################################*/
	Configure_Can1_Filter();

	/*##-3- Start the CAN peripheral ###########################################*/
	if (HAL_CAN_Start(&hcan1) != HAL_OK)
	{
		/* Start Error */
		Error_Handler();
	}

	/*##-4- Activate CAN RX notification #######################################*/
	if (HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK)
	{
		/* Notification Error */
		Error_Handler();
	}

	/*##-5- Configure Transmission process #####################################*/
//	TxHeader.StdId = 0x321;

}

void BSP_CAN1_Transimt_Test(void){
	TxHeader.ExtId = 0x0C107060;
	TxHeader.RTR = CAN_RTR_DATA;
	TxHeader.IDE = CAN_ID_EXT;
	TxHeader.DLC = 8;
	TxHeader.TransmitGlobalTime = DISABLE;
	TxData[0] = 0x11;
	TxData[1] = 0x00;
	TxData[7] = 0x77;

	/* Start the Transmission process */
	if (HAL_CAN_AddTxMessage(&hcan1, &TxHeader, TxData, &TxMailbox) != HAL_OK)
	{
		/* Transmission Error */
		Error_Handler();
	}
}

void BSP_CAN1_Transmit(uint32_t ExtId,uint8_t TransimitData[]) {
	TxHeader.ExtId = ExtId;
	TxHeader.RTR = CAN_RTR_DATA;
	TxHeader.IDE = CAN_ID_EXT;
	TxHeader.DLC = 8;
	TxHeader.TransmitGlobalTime = DISABLE;

	/* Start the Transmission process */
	if (HAL_CAN_AddTxMessage(&hcan1, &TxHeader, TransimitData, &TxMailbox) != HAL_OK)
	{
		/* Transmission Error */
		can1TransmitErrorFlag++;
		Error_Handler();
	}
}

void BSP_CAN2_Config(void) {
	/*##-1- Configure the CAN peripheral #######################################*/
	MX_CAN2_Init();
	/*##-2- Configure the CAN Filter ###########################################*/
	Configure_Can2_Filter();

	/*##-3- Start the CAN peripheral ###########################################*/
	if (HAL_CAN_Start(&hcan2) != HAL_OK)
	{
		/* Start Error */
		Error_Handler();
	}

	/*##-4- Activate CAN RX notification #######################################*/
	if (HAL_CAN_ActivateNotification(&hcan2, CAN_IT_RX_FIFO1_MSG_PENDING) != HAL_OK)
	{
		/* Notification Error */
		Error_Handler();
	}

	/*##-5- Configure Transmission process #####################################*/
//	TxHeader.StdId = 0x321;

}

void BSP_CAN2_TxTest(void){
	Tx2Header.ExtId = 0x0C107011;
	Tx2Header.RTR = CAN_RTR_DATA;
	Tx2Header.IDE = CAN_ID_EXT;
	Tx2Header.DLC = 8;
	Tx2Header.TransmitGlobalTime = DISABLE;
	Tx2Data[0] = 0x11;
	Tx2Data[1] = 0x00;
	Tx2Data[7] = 0x77;

	/* Start the Transmission process */
	if (HAL_CAN_AddTxMessage(&hcan2, &Tx2Header, Tx2Data, &Tx2Mailbox) != HAL_OK)
	{
		/* Transmission Error */
		Error_Handler();
	}
}

void BSP_CAN2_Tx(uint32_t ExtId,uint8_t TransimitData[]) {
	Tx2Header.ExtId = ExtId;
	Tx2Header.RTR = CAN_RTR_DATA;
	Tx2Header.IDE = CAN_ID_EXT;
	Tx2Header.DLC = 8;
	Tx2Header.TransmitGlobalTime = DISABLE;

	/* Start the Transmission process */
	if (HAL_CAN_AddTxMessage(&hcan2, &Tx2Header, TransimitData, &Tx2Mailbox) != HAL_OK)
	{
		/* Transmission Error */
		can2TransmitErrorFlag ++;
		Error_Handler();
	}
}
